package com.jhx.mall.dao.impl;

import com.jhx.mall.dao.BaseDao;
import com.jhx.mall.dao.DtsOrderDao;
import com.jhx.mall.entity.DtsAddress;
import com.jhx.mall.entity.DtsComment;
import com.jhx.mall.vo.DtsOrdersVo;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DtsOrderDaoImpl extends BaseDao implements DtsOrderDao {

    //订单查询
    @Override
    public List<DtsOrdersVo> selectMyOrder(int userId, String orderStatus) {
        List<DtsOrdersVo> list = new ArrayList<>();

        try {
            conn = getConnection();
            String sql = "select order_sn,order_status,orderStatusName,goods_name,pic_url,number,specifications,price,goods_price,actual_price" +
                    " from dts_order o inner join dts_order_goods g " +
                    "on o.id = order_id inner join dts_order_status s on o.order_status = s.orderStatus_id where user_id = "+userId+" ";

            if (orderStatus != null && !orderStatus.equals("")){
                sql += "and order_status = "+orderStatus+"";
            }

            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                DtsOrdersVo a = new DtsOrdersVo();
                a.setOrderSn(rs.getString("order_sn"));
                a.setOrderStatus(rs.getString("order_status"));
                a.setOrderStatusName(rs.getString("orderStatusName"));
                a.setGoodsName(rs.getString("goods_name"));
                a.setPicUrl(rs.getString("pic_url"));
                a.setNumber(rs.getInt("number"));
                a.setSpecifications(rs.getString("specifications"));
                a.setPrice(rs.getDouble("price"));
                a.setGoodsPrice(rs.getDouble("goods_price"));
                a.setActualPrice(rs.getDouble("actual_price"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, pstmt, conn);
        }
        return list;
    }

    //订单详情
    @Override
    public List<DtsOrdersVo> selectOrderDetail(int userId) {
        List<DtsOrdersVo> list = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "select user_id,order_sn,o.add_time,consignee,mobile,address,goods_name,pic_url,number,specifications,price,goods_price,actual_price" +
                    "freight_price,coupon_price" +
                    " from dts_order o inner join dts_order_goods g " +
                    "on o.id = order_id inner join dts_order_status s " +
                    " on o.order_status = s.orderStatus_id" +
                    " where user_id = "+userId+" ";

            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                DtsOrdersVo a = new DtsOrdersVo();
                a.setOrderSn(rs.getString("order_sn"));
                a.setOrderStatus(rs.getString("order_status"));
                a.setOrderStatusName(rs.getString("orderStatusName"));
                a.setGoodsName(rs.getString("goods_name"));
                a.setPicUrl(rs.getString("pic_url"));
                a.setNumber(rs.getInt("number"));
                a.setSpecifications(rs.getString("specifications"));
                a.setPrice(rs.getDouble("price"));
                a.setGoodsPrice(rs.getDouble("goods_price"));
                a.setActualPrice(rs.getDouble("actual_price"));
                a.setAddTime(rs.getString("add_time"));
                a.setConsignee(rs.getString("consignee"));
                a.setMobile(rs.getString("mobile"));
                a.setAddress(rs.getString("address"));
                a.setFreight_price(rs.getDouble("freight_price"));
                a.setCoupon_price(rs.getDouble("coupon_price"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, pstmt, conn);
        }
        return list;
    }

    //取消订单
    @Override
    public int quOrder(int id) {
        String sql = "update dts_order set order_status = 102 where id = "+id+"";
        return executeUpdate(sql);
    }

    //删除订单
    @Override
    public int delOrder(int id) {
        String sql = "update dts_order set deleted = 1 where id = "+id+"";
        return executeUpdate(sql);
    }

    //添加评论
    @Override
    public int addComment(DtsComment comment) {
        return 0;
    }
}
